layui.define(["table", "form", "upload", "admin"], function (exports) {
  var $ = layui.$,
    table = layui.table,
    form = layui.form,
    upload = layui.upload,
    admin = layui.admin;

  //
  table.render({
    elem: "#LAY-user-manage",
    url: "/admin/user/nickname_review_list",
    cols: [
      [
        { field: "id", width: 120, title: "ID", sort: true, fixed: "left" },
        { field: "account", title: "账号", width: 120 },
        {
          field: "nick_name",
          title: "原昵称",
          minWidth: 150,
          templet: function (d) {
            return decodeURI(d.nick_name);
          },
        },
        {
          field: "pending_nick_name",
          title: "待审核昵称",
          minWidth: 150,
          templet: function (d) {
            return decodeURI(d.pending_nick_name);
          },
        },
        {
          field: "apply_time",
          title: "申请时间",
          minWidth: 120,
        },
        {
          field: "admin_account",
          title: "管理员",
          minWidth: 100,
        },
        {
          title: "操作",
          width: 200,
          align: "center",
          fixed: "right",
          toolbar: "#table-userlist-operation",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-180",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //监听工具条
  table.on("tool(LAY-user-manage)", function (obj) {
    if (obj.event === "del") {
      layer.confirm("确定封禁/解封用户？", function (index) {
        var status = obj.data.status == 0 ? 1 : 0;
        admin.req({
          url: "/admin/user/del_post",
          data: { id: obj.data.id, status: status },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event === "edit") {
      layer.open({
        type: 2,
        title: "编辑用户",
        content: "/admin/user/edit?id=" + obj.data.id,
        maxmin: true,
        area: ["500px", "450px"],
      });
    } else if (obj.event == "setAdmin") {
      layer.confirm("确定将改用户设为房间管理？", function (index) {
        admin.req({
          url: "/admin/user/setAdmin",
          data: { id: obj.data.id },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event == "delAdmin") {
      layer.msg("请到房间管理员列表页面取消管理身份", { icon: 5 });
    } else if (obj.event == "bannerd") {
      layer.confirm("确定禁言该用户吗?", function (index) {
        admin.req({
          url: "/admin/user/setBanned",
          data: { id: obj.data.id },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event == "unbannerd") {
      layer.msg("请到禁言列表页面解除禁言", { icon: 5 });
    } else if (obj.event == "set_allow") {
      layer.confirm("确定通过该昵称修改吗?", function (index) {
        admin.req({
          url: "/admin/user/set_nickname_review",
          data: {
            uid: obj.data.id,
            status: 1,
            nickname: obj.data.pending_nick_name,
          },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    } else if (obj.event == "reject") {
      layer.confirm("确定拒绝该昵称修改吗?", function (index) {
        admin.req({
          url: "/admin/user/set_nickname_review",
          data: { uid: obj.data.id, status: 2 },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-manage");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    }
  });

  form.on("submit(user-add)", function (obj) {
    var data = obj.field;
    layer.load();
    //提交
    admin.req({
      url: "/admin/user/add_post",
      data: data,
      success: function (res) {
        if (res.code == 0) {
          layer.msg(res.msg, { icon: 1 });
          setTimeout(function () {
            window.location.reload();
          }, 1200);
        } else {
          setTimeout(function () {
            window.location.reload();
          }, 2200);
        }
      },
      done: function () {
        layer.closeAll("loading");
      },
    });
    return false;
  });

  //上传头像
  var avatarSrc = $("#LAY_avatarSrc");
  upload.render({
    url: "/admin/index/noOperation",
    elem: "#LAY_avatarUpload",
    auto: true,
    choose: function (obj) {
      layer.load();
      var choosefile;
      obj.preview(function (index, file, result) {
        choosefile = file;
      });
      admin.req({
        url: "/admin/user/signForCos",
        success: function (res) {
          if (res.code == 0) {
            var cos = new COS({
              getAuthorization: function (options, callback) {
                var credentials = res.data.credentials;
                callback({
                  TmpSecretId: credentials.tmpSecretId,
                  TmpSecretKey: credentials.tmpSecretKey,
                  XCosSecurityToken: credentials.sessionToken,
                  ExpiredTime: res.data.expiredTime,
                });
              },
              Domain:res.data.imgDomain,
            });
            cos.putObject(
              {
                Bucket: res.data.bucket,
                Region: res.data.region,
                Key: "images/" + res.data.filename + choosefile.name,
                Body: choosefile,
              },
              function (err, data) {
                layer.closeAll("loading");
                if (err) {
                  layer.msg("上传失败", { icon: 5 });
                } else {
                  if (data.statusCode == 200) {
                    avatarSrc.val("https://" + data.Location);
                  } else {
                    layer.msg("图片上传失败", { icon: 5 });
                  }
                }
              }
            );
          } else {
            layer.closeAll("loading");
            layer.msg(res.msg, { icon: 5 });
          }
        },
        error: function () {
          layer.closeAll("loading");
        },
      });
    },
  });

  //查看头像
  admin.events.avartatPreview = function (othis) {
    var src = avatarSrc.val();
    if (src) {
      layer.photos({
        photos: {
          title: "查看头像", //相册标题
          data: [
            {
              src: src, //原图地址
            },
          ],
        },
        shade: 0.01,
        closeBtn: 1,
        anim: 5,
      });
    }
  };

  //上传封面图
  var coverImgSrc = $("#LAY_coverImgSrc");
  upload.render({
    url: "/admin/index/noOperation",
    elem: "#LAY_coverImgUpload",
    auto: true,
    choose: function (obj) {
      layer.load();
      var choosefile;
      obj.preview(function (index, file, result) {
        choosefile = file;
      });
      admin.req({
        url: "/admin/user/signForCos",
        success: function (res) {
          if (res.code == 0) {
            var cos = new COS({
              getAuthorization: function (options, callback) {
                var credentials = res.data.credentials;
                callback({
                  TmpSecretId: credentials.tmpSecretId,
                  TmpSecretKey: credentials.tmpSecretKey,
                  XCosSecurityToken: credentials.sessionToken,
                  ExpiredTime: res.data.expiredTime,
                });
              },
            });
            cos.putObject(
              {
                Bucket: res.data.bucket,
                Region: res.data.region,
                Key: "images/" + res.data.filename + choosefile.name,
                Body: choosefile,
              },
              function (err, data) {
                layer.closeAll("loading");
                if (err) {
                  layer.msg("上传失败", { icon: 5 });
                } else {
                  if (data.statusCode == 200) {
                    coverImgSrc.val("https://" + data.Location);
                  } else {
                    layer.msg("图片上传失败", { icon: 5 });
                  }
                }
              }
            );
          } else {
            layer.closeAll("loading");
            layer.msg(res.msg, { icon: 5 });
          }
        },
      });
    },
  });

  //查看封面图
  admin.events.coverImgPreview = function (othis) {
    var src = coverImgSrc.val();
    if (src) {
      layer.photos({
        photos: {
          title: "查看头像", //相册标题
          data: [
            {
              src: src, //原图地址
            },
          ],
        },
        shade: 0.01,
        closeBtn: 1,
        anim: 5,
      });
    }
  };

  //查看照片
  admin.events.photosPreview = function (othis) {
    let urls = othis.attr("value");
    let urlarr = new Array();
    $.each(urls.split(","), function (i, val) {
      urlarr.push({ src: val });
    });
    layer.photos({
      photos: {
        title: "查看照片", //相册标题
        data: urlarr,
      },
      shade: 0.01,
      closeBtn: 1,
      anim: 5,
    });
  };

  ///////////////////////////////////////////////  report ////////////////////////////////////////////////

  table.render({
    elem: "#LAY-user-report-manage",
    url: "/admin/user/getReportList",
    cols: [
      [
        { field: "id", width: 100, title: "ID", sort: true, fixed: "left" },
        {
          field: "",
          title: "举报用户",
          width: 200,
          templet: function (d) {
            return d.user.nick_name + "(" + d.uid + ")";
          },
        },
        {
          field: "",
          title: "被举报用户",
          width: 200,
          templet: function (d) {
            return d.anchor.nick_name + "(" + d.anchor.id + ")";
          },
        },
        { field: "title", title: "举报理由", width: 200 },
        { field: "content", title: "举报内容", minWidth: 200 },
        {
          field: "",
          title: "图片",
          width: 80,
          templet: function (d) {
            if (d.img_urls && d.img_urls.length > 0) {
              let images = d.img_urls.split(",");
              return (
                '<img style="display: inline-block; width: auto; height: 100%;" src= "' +
                images[0] +
                '" layadmin-event="photosPreview" value="' +
                d.img_urls +
                '">'
              );
            }
            return "";
          },
        },
        {
          title: "操作",
          width: 150,
          align: "center",
          fixed: "right",
          toolbar: "#table-userlist-operation",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //监听工具条
  table.on("tool(LAY-user-report-manage)", function (obj) {
    console.log(obj);
    if (obj.event === "view") {
      var index = layer.open({
        type: 2,
        title: "短视频",
        content: "/admin/user/edit?id=" + obj.data.userid,
        maxmin: true,
        area: ["600px", "550px"],
      });
      layer.full(index);
    }
  });

  ///////////////////////////////////////////////  room-admin ////////////////////////////////////////////////

  table.render({
    elem: "#LAY-user-room-admin",
    url: "/admin/user/getRoomAminList",
    cols: [
      [
        { field: "id", title: "ID", sort: true, fixed: "left" },
        { field: "mgrid", title: "管理员ID" },
        {
          field: "anchorid",
          title: "主播ID",
          templet: function (d) {
            if (d.anchorid == 0) {
              return "<font color='red'>所有主播房间</font>";
            } else {
              return d.anchorid;
            }
          },
        },
        { field: "create_time", title: "创建时间" },
        {
          title: "操作",
          align: "center",
          fixed: "right",
          toolbar: "#table-useradmin-operation",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //监听工具条
  table.on("tool(LAY-user-room-admin)", function (obj) {
    if (obj.event === "setAdmin") {
      layer.confirm("确定取消该用户？", function (index) {
        admin.req({
          url: "/admin/user/setAdmin",
          data: { id: obj.data.mgrid },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-room-admin");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    }
  });

  ///////////////////////////////////////////////  room-banned ////////////////////////////////////////////////
  table.render({
    elem: "#LAY-user-room-banned",
    url: "/admin/user/getBannedList",
    cols: [
      [
        { field: "id", title: "ID", sort: true, fixed: "left" },
        { field: "uid", title: "用户ID" },
        {
          field: "mgrid",
          title: "管理员ID",
          templet: function (d) {
            if (d.anchorid == 0) {
              return "后台管理员";
            } else {
              return d.mgrid;
            }
          },
        },
        {
          field: "anchorid",
          title: "主播ID",
          templet: function (d) {
            if (d.anchorid == 0) {
              return "<font color='red'>所有主播房间</font>";
            } else {
              return d.anchorid;
            }
          },
        },
        { field: "create_time", title: "创建时间" },
        {
          title: "操作",
          align: "center",
          fixed: "right",
          toolbar: "#table-userbanned-operation",
        },
      ],
    ],
    page: true,
    limit: 20,
    height: "full-140",
    text: "对不起，加载出现异常！",
    even: true,
  });

  //监听工具条
  table.on("tool(LAY-user-room-banned)", function (obj) {
    if (obj.event === "setBanned") {
      layer.confirm("确定解除禁言？", function (index) {
        admin.req({
          url: "/admin/user/setBanned",
          data: { id: obj.data.uid },
          success(res) {
            if (res.code == 0) {
              table.reload("LAY-user-room-banned");
              layer.msg(res.msg, { icon: 1 });
            } else {
              layer.msg(res.msg, { icon: 5 });
            }
          },
        });
      });
    }
  });

  exports("nickname_review", {});
});
